Make mnemonic RNG selection explicit - #1013
Merged
Merged
Conversation
Mnemonic generation is infrequent, so source randomness directly from OS entropy instead of initializing a thread-local generator. Also given recent events, better have this more explicitly defined than in a dep. Generated with assistance from OpenAI Codex.
|
👋 Thanks for assigning @tnull as a reviewer! |
tankyleo
approved these changes
Aug 5, 2026
tankyleo
left a comment
Contributor
There was a problem hiding this comment.
@tnull let us know what you think, this gets us back to our choice of RNG following this PR #683
I'm also thinking we should apply this patch on rust-bip39 too:
diff --git a/src/lib.rs b/src/lib.rs
index ea56c05..69f4def 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -263,7 +263,7 @@ impl Mnemonic {
/// ```
/// use bip39::{Mnemonic, Language};
///
- /// let mut rng = bip39::rand::thread_rng();
+ /// let mut rng = bip39::rand::rngs::OsRng;
/// let m = Mnemonic::generate_in_with(&mut rng, Language::English, 24).unwrap();
/// ```
#[cfg(feature = "rand_core")]
@@ -297,7 +297,7 @@ impl Mnemonic {
/// ```
#[cfg(feature = "rand")]
pub fn generate_in(language: Language, word_count: usize) -> Result<Mnemonic, Error> {
- Mnemonic::generate_in_with(&mut rand::thread_rng(), language, word_count)
+ Mnemonic::generate_in_with(&mut rand::rngs::OsRng, language, word_count)
}
/// Generate a new [Mnemonic] in English.
@@ -727,7 +727,7 @@ mod tests {
fn test_generate() {
let _ = Mnemonic::generate(24).unwrap();
let _ = Mnemonic::generate_in(Language::English, 24).unwrap();
- let _ = Mnemonic::generate_in_with(&mut rand::thread_rng(), Language::English, 24).unwrap();
+ let _ = Mnemonic::generate_in_with(&mut rand::rngs::OsRng, Language::English, 24).unwrap();
}
#[cfg(feature = "rand")]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mnemonic generation is infrequent, so source randomness directly from OS entropy instead of initializing a thread-local generator. Also given recent events, better have this more explicitly defined than in a dep.